<?php

namespace App\Services\Admin;

use \App\Models\UserIntegral;
use App\Models\UserSubscribe;

class Report extends Base
{
    protected $start_time = '';
    protected $end_time = '';

    public function __construct(UserIntegral $userIntegral, UserSubscribe $userSubscribe)
    {
        $start_time = request()->start_time??'';
        $end_time = request()->end_time??'';

        if(!empty($start_time) && !empty($end_time)){
            $this->start_time = "{$start_time} 00:00:00";
            $this->end_time = "{$end_time} 00:00:00";
        }else {
            $this->end_time = date("Y-m-d 00:00:00", time());
            $this->start_time = date("Y-m-d 00:00:00", strtotime("-7 day", strtotime("" . $this->end_time . "")));
        }

        $this->user_integral = $userIntegral;
        $this->user_subscribe = $userSubscribe;
    }

    public function time()
    {
        return ['start_time'=>explode(' ',$this->start_time)[0],'end_time'=>explode(' ',$this->end_time)[0]];
    }

    //充值统计
    public function recharge_statistics()
    {
        $where[] = ['status',2];
        $where[] = ['created_at','>=',$this->start_time];
        $where[] = ['created_at','<=',$this->end_time];
        $user_integral = $this->user_integral->where($where)->get()->toArray();

        $result = [];

        foreach ($user_integral as $v){

            $result_key = date("Ymd", strtotime("{$v['created_at']}"));

            if(!isset($result[$result_key]))$result[$result_key] = 0;

            $result[$result_key] += $v['integral'];

        }

        ksort($result);

        return $result;
    }

    //约课统计
    public function appointment_statistics()
    {
        $where[] = ['created_at','>=',$this->start_time];
        $where[] = ['created_at','<=',$this->end_time];
        $where[] = ['subscribe_status',5];
        $user_subscribe = $this->user_subscribe->where($where)->get()->toArray();

        $result = [];

        foreach ($user_subscribe as $v){
            if(!isset($result[$v['class_name']]))$result[$v['class_name']] = 0;
            $result[$v['class_name']]++;
        }

        $appointment_format = [];

        foreach ($result as $k=>$v){
            $input['label'] = $k." ({$v})";
            $input['data'] = $v;

            $appointment_format[] = $input;
        }

        return json_encode($appointment_format);
    }

    //老师统计
    public function teacher_statistics()
    {
        $where[] = ['created_at','>=',$this->start_time];
        $where[] = ['created_at','<=',$this->end_time];
        $where[] = ['subscribe_status',5];
        $user_subscribe = $this->user_subscribe->where($where)->get()->toArray();

        $result = [];

        foreach ($user_subscribe as $v){
            if(!isset($result[$v['teacher_name']]))$result[$v['teacher_name']] = 0;
            $result[$v['teacher_name']]++;
        }

        $appointment_format = [];

        foreach ($result as $k=>$v){
            $input['label'] = $k." ({$v})";
            $input['data'] = $v;

            $appointment_format[] = $input;
        }

        return json_encode($appointment_format);
    }
}
